
package Trans_Virtual_Sensor.v1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 
 * adapted from http://www.ibm.com/developerworks/opensource/library/os-android-sensor/index.html#download
 * @author ga-youngjoung
 *
 */

public class VirtualSensor extends Activity {
	
	private static final String TAG = "Virtual Sensor";
	private static final int[] myArrayOfTypes = {Sensor.TYPE_ACCELEROMETER,
		Sensor.TYPE_ORIENTATION,
		Sensor.TYPE_MAGNETIC_FIELD,
		Sensor.TYPE_LIGHT};
	private static final String[] myArrayOfNames = {"acc", "orient", "magn_field", "light"};
	
	public MediaRecorder myRec = null;
	private SensorManager mySM = null;
	private ArrayList<Thread> myListOfThreads = new ArrayList<Thread>();
	private ArrayList<SenListener> myListOfListeners = new ArrayList<SenListener>();
	private Button startRecording = null;
	private Button stopRecording = null;
	private TextView filename = null;
	private TextView status = null;
	File audiofile;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         
        myRec = new MediaRecorder();
        
        startRecording = (Button)findViewById(R.id.startrecording);
        stopRecording = (Button)findViewById(R.id.stoprecording);
        filename = (TextView) findViewById(R.id.text);
        status = (TextView) findViewById(R.id.status);
        
        mySM = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
        
        startRecording.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View v) {
        		
        		try {
        			for (int i = 0; i < myArrayOfTypes.length; i++) {
            			myListOfListeners.add(new SenListener(filename.getText().toString()+"_"+myArrayOfNames[i],
            									myArrayOfTypes[i], mySM));
            			myListOfThreads.add(new Thread(myListOfListeners.get(i), myArrayOfNames[i]));
            		}
        			startRecording.setEnabled(false);
        			stopRecording.setEnabled(true);
        			stopRecording.requestFocus();
        			startRecording();
        		} catch (Exception ee) {
        			Log.e(TAG,"Caught io exception " + ee.getMessage());
        			status.setText("Caught io exception " + ee.getMessage());
        		}
        	}
        });
        
        stopRecording.setOnClickListener(new View.OnClickListener(){
        	public void onClick(View v) {
        		startRecording.setEnabled(true);
        		stopRecording.setEnabled(false);
        		startRecording.requestFocus();
        		stopRecording();
        		status.setText("write is complete: " + filename.getText().toString());
        	}
         });
        
	   stopRecording.setEnabled(false);
	   startRecording.setEnabled(true);
    }
    
    protected void startRecording() throws IOException {
    	for (int i = 0; i < myListOfThreads.size(); i++) {
    		myListOfThreads.get(i).start();
    	}
    	myRec.setAudioSource(MediaRecorder.AudioSource.MIC);
        //TODO: figure out how to get .wav format, and what format is best
        myRec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        if (audiofile == null) {
	          File sampleDir = Environment.getExternalStorageDirectory();
	          audiofile = new File(sampleDir, filename.getText().toString() + ".3gp");
        }
        status.setText("writing with name: "+ filename.getText().toString());
        myRec.setOutputFile(audiofile.getAbsolutePath());
        myRec.prepare();
        myRec.start();
    }

    public void onConfigurationChanged(Configuration newConfig) {
    	//do nothing
    }
    
    protected void stopRecording() {
    	for (int i = 0; i < myListOfThreads.size(); i++) {
    		myListOfListeners.get(i).stop();
    		myListOfThreads.get(i).stop();
    	}
    	myRec.stop();
        myRec.release();
    }
    
}